package com.winspread.ui.progress

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import androidx.databinding.BindingAdapter
import com.winspread.ui.R
import com.winspread.ui.utils.SizeUtils
import com.winspread.ui.utils.SizeUtils.dp2px
import com.winspread.ui.utils.SizeUtils.sp2px

/**
 * 电量控件
 *
 * // xml代码
 * <com.winspread.wirelessmeasurement.widget.ElectricityView
 * android:id="@+id/ele"
 * android:layout_width="60dp"
 * android:layout_height="22dp"
 * </com.winspread.wirelessmeasurement.widget.ElectricityView>
 * // 设置电量
 * binding.ele.setEleNumber(78);
 */
class ElectricityView : View {
    // 可设置属性
    private var leftTextSize = 0f
    private var leftTextColor = 0
    private var spacing = 0f
    private var eleColor = 0
    private var eleNumber = 0
    private var eleWidth = 0f
    private var eleHeight = 0f
    private var eleInterWidth = 0f
    private var eleInterHeight = 0f
    private var layoutwidth = 0
    private var layoutHeight = 0
    private var eleDirection = true

    // 画笔
    // 文字
    var paintText: Paint? = null

    // 电池头
    var paintTop: Paint? = null

    // 电池一圈
    var paintEle: Paint? = null

    // 内部电量
    var paintEleInter: Paint? = null

    constructor(context: Context?) : super(context)

    @JvmOverloads
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        // 创建控件的时候获取属性
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ElectricityView)
        leftTextSize = typedArray.getDimension(
            R.styleable.ElectricityView_left_text_size,
            DEFULDE_LEFT_TEXT_SIZE
        )
        leftTextColor = typedArray.getColor(
            R.styleable.ElectricityView_left_text_color,
            DEFULDE_LEFT_TEXT_COLOR
        )
        spacing =
            typedArray.getDimension(R.styleable.ElectricityView_spacing, DEFULDE_SPACING).toInt()
                .toFloat()
        eleColor = typedArray.getColor(R.styleable.ElectricityView_ele_color, DEFULDE_ELE_COLOR)
        eleNumber = typedArray.getInt(R.styleable.ElectricityView_ele_number, DEFULDE_ELE_NUMBER)
        eleWidth = typedArray.getDimension(R.styleable.ElectricityView_ele_width, DEFULDE_ELE_WIDTH)
            .toInt()
            .toFloat()
        eleHeight =
            typedArray.getDimension(R.styleable.ElectricityView_ele_height, DEFULDE_ELE_HEIGHT)
                .toInt()
                .toFloat()
        eleInterWidth = typedArray.getDimension(
            R.styleable.ElectricityView_ele_inter_width,
            DEFULDE_ELE_INTER_WIDTH
        ).toInt()
            .toFloat()
        eleInterHeight = typedArray.getDimension(
            R.styleable.ElectricityView_ele_inter_height,
            DEFULDE_ELE_INTER_HEIGHT
        ).toInt()
            .toFloat()
        layoutwidth =
            typedArray.getLayoutDimension(R.styleable.ElectricityView_android_layout_width, -1)
        layoutHeight =
            typedArray.getLayoutDimension(R.styleable.ElectricityView_android_layout_height, -2)
        eleDirection =
            typedArray.getBoolean(R.styleable.ElectricityView_ele_direction, true)
        // 画笔
        // 文字
        paintText = Paint()
        paintText!!.textSize = leftTextSize
        paintText!!.color = leftTextColor
        paintText!!.isAntiAlias = true
        // 电池头
        paintTop = Paint()
        paintTop!!.color = eleColor
        paintTop!!.style = Paint.Style.FILL
        paintTop!!.isAntiAlias = true

        // 电池一圈
        paintEle = Paint()
        paintEle!!.color = eleColor
        paintEle!!.style = Paint.Style.STROKE
        paintEle!!.strokeWidth = dp2px(2f).toFloat()
        paintEle!!.isAntiAlias = true
        // 内部电量
        paintEleInter = Paint()
        paintEleInter!!.style = Paint.Style.FILL
        paintEleInter!!.isAntiAlias = true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (eleDirection){
            // 电池一圈
            val eleleft =dp2px(2.5f).toFloat()
            val eletop = (layoutHeight-eleWidth)/2f
            val eleright =eleleft + eleHeight
            val elebottom = (layoutHeight+eleWidth)/2f
            val roundnum = (eleWidth - eleInterWidth) / 2

            canvas.drawRoundRect(
                eleleft,
                eletop,
                eleright,
                elebottom,
                roundnum,
                roundnum,
                paintEle!!
            )
            // 电池头
            val eletopleft = eleleft + eleHeight
            val eletoptop =  layoutHeight/2-eleWidth/3
            val eletopright = eletopleft+dp2px(3f)
            val eletopbottom = layoutHeight/2+eleWidth/3
            canvas.drawRoundRect(
                eletopleft,
                eletoptop,
                eletopright,
                eletopbottom,
                roundnum / 2,
                roundnum / 2,
                paintTop!!
            )

            // 内部电量
            val eleinterleft = eleleft + roundnum
            val eleintertop = eletop + roundnum
            var eleinterright = eleright - roundnum
            val eleinterbottom = elebottom - roundnum
            val offset = (eleinterright - eleinterleft) / 100 * eleNumber
            eleinterright = offset+eleinterleft
            canvas.drawRoundRect(
                eleinterleft,
                eleintertop,
                eleinterright,
                eleinterbottom,
                roundnum / 2,
                roundnum / 2,
                paintEleInter!!
            )


            //电量文字画图
            val elnum = "$eleNumber%"
            val elnumRect = Rect()
            paintText!!.getTextBounds(elnum, 0, elnum.length, elnumRect)
            canvas.drawText(
                elnum,
                eletopright+  dp2px(2f).toFloat(),
                ((elnumRect.height() + height) / 2 - elnumRect.height() / 15).toFloat(),
                paintText!!
            )
        }else{
            val elnum = "$eleNumber%"
            val elnumRect = Rect()
            paintText!!.getTextBounds(elnum, 0, elnum.length, elnumRect)
            canvas.drawText(
                elnum,
                dp2px(2f).toFloat(),
                ((elnumRect.height() + height) / 2 - elnumRect.height() / 15).toFloat(),
                paintText!!
            )

            val eleleft: Float =
                elnumRect.width() + spacing + dp2px(2f)
            val eletop: Float = eleHeight / 10 + dp2px(2f)
            val roundnum = (eleWidth - eleInterWidth) / 2
            val elebottom: Float = eleHeight +  dp2px(2f)
            canvas.drawRoundRect(
                eleleft, eletop, eleleft + eleWidth, elebottom, roundnum, roundnum,
                paintEle!!
            )

            val eletopleft = eleleft + eleWidth / 3
            val eletoptop: Float =  dp2px(1f).toFloat()
            val eletopright = eleleft + eleWidth / 3 * 2
            val eletopbottom: Float = eleHeight / 10 +  dp2px(2f)
            canvas.drawRoundRect(
                eletopleft, eletoptop, eletopright, eletopbottom, roundnum / 2, roundnum / 2,
                paintTop!!
            )

            val eleinterleft = eleleft + roundnum
            var eleintertop = eletop + roundnum
            val eleinterright = eleinterleft + eleInterWidth
            val eleinterbottom = elebottom - roundnum

            val offset = (eleinterbottom - eleintertop) / 100 * (100 - eleNumber)
            eleintertop += offset

            canvas.drawRoundRect(
                eleinterleft,
                eleintertop,
                eleinterright,
                eleinterbottom,
                roundnum / 2,
                roundnum / 2,
                paintEleInter!!
            )
        }


    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(layoutwidth, layoutHeight)
    }

    fun setEleNumber(eleNumber: Int) {
        this.eleNumber = eleNumber
        invalidate()
    }

    fun setEleDirection(eleDirection:Boolean){
        this.eleDirection = eleDirection
        invalidate()
    }

    fun setPowerStatus(powerStatus: Boolean) {
        if (powerStatus) {
            paintEleInter!!.color = Color.parseColor("#2CC165")
        } else {
            paintEleInter!!.color = eleColor
        }
    }

    companion object {
        // 属性介绍
        // left_text_size 左边字体大小
        // left_text_color 左边字体颜色
        // spacing 左右间距
        // ele_color 电量颜色
        // ele_number 电量
        // ele_width 电池宽度
        // ele_height 电池高度
        // ele_inter_width 电量宽度
        // ele_inter_height 电量高度
        // 默认值
        private val DEFULDE_LEFT_TEXT_SIZE = sp2px(14f).toFloat()
        private val DEFULDE_LEFT_TEXT_COLOR = Color.parseColor("#1F2336")
        private val DEFULDE_SPACING = dp2px(6f).toFloat()
        private val DEFULDE_ELE_COLOR = Color.parseColor("#1F2336")
        private const val DEFULDE_ELE_NUMBER = 100
        private val DEFULDE_ELE_WIDTH = dp2px(10f).toFloat()
        private val DEFULDE_ELE_HEIGHT = dp2px(18f).toFloat()
        private val DEFULDE_ELE_INTER_WIDTH = dp2px(6f).toFloat()
        private val DEFULDE_ELE_INTER_HEIGHT = dp2px(13f).toFloat()
    }
}


@BindingAdapter(value = ["ele_number"], requireAll = false)
fun  ElectricityView.setEleNumber( eleNumber: Int){
    this.setEleNumber(eleNumber)
}

@BindingAdapter(value = ["ele_direction"], requireAll = false)
fun  ElectricityView.setEleDirection( eleDirection: Boolean){
    this.setEleDirection(eleDirection)
}